package com.jeesite.modules.rt.service;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.rt.entity.RtDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.rt.entity.RtDeviceLine;
import com.jeesite.modules.rt.dao.RtDeviceLineDao;

/**
 * 网络信息Service
 * @author 李寒冰
 * @version 2024-09-26
 */
@Service
public class RtDeviceLineService extends CrudService<RtDeviceLineDao, RtDeviceLine> {


	@Autowired
	private RtDeviceService rtDeviceService;

	/**
	 * JSON转设备实例
	 * @param jsonObject
	 */
	public void jsonToDeviceLine(JSONObject jsonObject){
		String sn = jsonObject.getString("sn");
		RtDeviceLine deviceLine= getDeviceLineBySn(sn);

		RtDeviceLine device = jsonObject.toJavaObject(RtDeviceLine.class);
		device.setLineRefreshTime(new Date());
		//省份城市
		String provinceCity = "";
		String province = jsonObject.getString("province");
		if(StringUtils.isNotBlank(province)){
			provinceCity += province;
		}
		String city = jsonObject.getString("city");
		if(StringUtils.isNotBlank(province)){
			provinceCity += city;
		}
		device.setProvinceCity(provinceCity);
		//测速
		JSONArray uploadList = jsonObject.getJSONArray("uploadList");
		if(uploadList!=null && uploadList.size()>0){
			JSONObject jsonObject1 = uploadList.getJSONObject(0);
			if(jsonObject1!=null ){
				device.setSpeed(jsonObject1.getDouble("speed")!=null?jsonObject1.getDouble("speed").toString():"0");
			}
		}
		if(deviceLine!=null){
			device.setId(deviceLine.getId());
		}
		String ispCode = device.getIspCode();
		String nat = device.getNat();
		RtDevice rtDevice = rtDeviceService.getDeviceBySn(sn);
		rtDevice.setIspCode(Integer.valueOf(ispCode));
		rtDevice.setNat(nat);
		rtDeviceService.save(rtDevice);
		save(device);
	}

	/**
	 * 获取单条数据
	 * @param sn
	 * @return
	 */
	public RtDeviceLine getDeviceLineBySn(String sn) {
		RtDeviceLine rtDeviceLine = new RtDeviceLine();
		rtDeviceLine.setSn(sn);
		List<RtDeviceLine> list = findList(rtDeviceLine);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}

	
	/**
	 * 获取单条数据
	 * @param rtDeviceLine
	 * @return
	 */
	@Override
	public RtDeviceLine get(RtDeviceLine rtDeviceLine) {
		return super.get(rtDeviceLine);
	}
	
	/**
	 * 查询分页数据
	 * @param rtDeviceLine 查询条件
	 * @param rtDeviceLine page 分页对象
	 * @return
	 */
	@Override
	public Page<RtDeviceLine> findPage(RtDeviceLine rtDeviceLine) {
		return super.findPage(rtDeviceLine);
	}
	
	/**
	 * 查询列表数据
	 * @param rtDeviceLine
	 * @return
	 */
	@Override
	public List<RtDeviceLine> findList(RtDeviceLine rtDeviceLine) {
		return super.findList(rtDeviceLine);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param rtDeviceLine
	 */
	@Override
	@Transactional
	public void save(RtDeviceLine rtDeviceLine) {

		super.save(rtDeviceLine);
	}
	
	/**
	 * 更新状态
	 * @param rtDeviceLine
	 */
	@Override
	@Transactional
	public void updateStatus(RtDeviceLine rtDeviceLine) {
		super.updateStatus(rtDeviceLine);
	}
	
	/**
	 * 删除数据
	 * @param rtDeviceLine
	 */
	@Override
	@Transactional
	public void delete(RtDeviceLine rtDeviceLine) {
		super.delete(rtDeviceLine);
	}
	
}